<?php include 'inc/header.php';?>
<?php
 $name = $email = $courseName = $content = '';
 $nameErr = $emailErr = $courseNameErr = $contentErr = '';
 if (isset($_POST['submit'])) {
    if (empty($_POST['name'])){
        $nameErr="姓名为必填项";
    }
    else{
        $name=filter_input(INPUT_POST, 'name',FILTER_SANITIZE_FULL_SPECIAL_CHARS); 
    }

    if (empty($_POST['email'])){
        $emailErr="邮箱为必填项";
    }
    else{
        $email=filter_input(INPUT_POST, 'email',FILTER_SANITIZE_FULL_SPECIAL_CHARS); 
    }

    if (empty($_POST['course'])){
        $courseNameErr="课程名称为必填项";
    }
    else{
        $courseName=filter_input(INPUT_POST, 'course',FILTER_SANITIZE_FULL_SPECIAL_CHARS); 
    }

    if (empty($_POST['body'])){
        $contentErr="课程内容为必填项";
    }
    else{
        $content=filter_input(INPUT_POST, 'body',FILTER_SANITIZE_FULL_SPECIAL_CHARS); 
    }
    }
    echo $nameErr;
    echo $name;
    if (empty($nameErr) && empty($emailErr) && empty($courseNameErr) && empty($contentErr)) {
        $sql = "INSERT INTO review (name, email, course_name, content) 
        VALUES('$name', '$email', '$courseName', '$content')";
    if (mysqli_query($conn, $sql)) {
        header('Location: reviews.php');
        } 
        else {
            echo "出错：" . mysqli_error($conn);
        }
}

?>
    <img src="img/logo.png" class="w-25 mb-3" alt="">
    <h2>课程评价</h2>
    <p class="lead text-center">为你所上的课程留下评价</p>
    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="POST" class="mt-4 w-75">
    <div class="mb-3">
    <label for="name" class="form-label">你的姓名</label>
    <input type="text" class="form-control <?php echo $nameErr ?'is-invalid' : null;?>" id="name" name="name">
    <div class="invalid-feedback">
        <?php echo $nameErr; ?>
    </div>
    </div>
    <div class="mb-3">
    <label for="email" class="form-label">你的邮箱</label>
    <input type="email" class="form-control <?php echo $emailErr ?'is-invalid' : null;?>" id="email" name="email">
    <div class="invalid-feedback">
        <?php echo $emailErr; ?>
    </div>

</div>
    <div class="mb-3">
    <label for="course" class="form-label">课程名称</label>
    <input type="text" class="form-control <?php echo $courseNameErr ?'is-invalid' : null;?>" id="course" name="course">
    <div class="invalid-feedback">
        <?php echo $courseNameErr; ?>
    </div>    
</div>
    <div class="mb-3">
        <label for="body" class="form-label">评价内容</label>
        <textarea class="form-control <?php echo $contentErr ?'is-invalid' : null;?>" id="body" name="body" row=""></textarea>
        <div class="invalid-feedback">
            <?php echo $contentErr; ?>
        </div>    
    </div>
        <div class="mb-3">
        <input type="submit" name="submit" value="提交评价" class="form-control">
        </div>
        </form>
<?php include 'inc/footer.php';?>
      <!-- 完成 -->
   